<?php	 

class Article extends Action{	
private $table='demo';

/*
+------------------------------------------------------------------------------
*/
	public function one(){
		
		$get=$this->_get('id:isInt');//获取URL传递的ID，必须为整数
		$one= $this->C()->findOne($this->table,"select * from TB where id='$get[id]' ");
		if(empty($one))$this->go('back','该记录不存在',5);//不存在则报错误,5秒返回上一步
		$clicks=$one['clicks']+1;
		$this->C(3600)->update($this->table,array('clicks'=>$clicks),array('id'=>$one['id']));//更新点击数，3600秒代表3600秒后再更新缓存
		$this->assign(array('one'=>$one));	 
		$this->show('/tpl_article/one'); 	

	}
/*
+------------------------------------------------------------------------------
*/	
	public function add(){
		if(empty($_POST)){
			$this->show('/tpl_article/form'); 
		}else{
			$post=$this->_post('title,content');
			$sendTime=time();
			$title=cutstr($post['title'],40);//截取字符
			$content=cutstr($post['content'],400,'...');
			$insertId=$this->C()->insert($this->table,array('title'=>$title,'content'=>$content,'sendTime'=>$sendTime));//插入数据
			if($insertId>0){
				$this->go('/');//返回首页
			}else{
				$this->go('back','添加失败',5);	 
			}	
		}
	}

/*
+------------------------------------------------------------------------------
*/	
	public function _update(){
		
		
		$get=$this->_get('id:isInt');
		$id=$get['id'];
		$one= $this->C()->findOne($this->table,"select * from TB where id='$id' ");
		$this->assign(array('one'=>$one));	
		$this->show('/tpl_article/form');	
	}
/*
+------------------------------------------------------------------------------
*/	
	public function update(){
		
		$post=$this->_post('id,title,content');
		$title=cutstr($post['title'],40);
		$content=cutstr($post['content'],400,'...');
		$result= $this->C()->update($this->table,array('title'=>$title,'content'=>$content),array('id'=>$post['id']));							
		if($result>=0 ){
			$this->go('back');
		}else{
			$this->go('back','修改失败',5);
		}
	}
/*
+------------------------------------------------------------------------------
*/	
	public function del(){

		$get=$this->_get('id:isInt');
		$id=$get['id'];
		$result=$this->C()->delete($this->table,array('id'=>$id));	
		if($result>0 ){
			$this->go('back');
		}else{
			$this->go('back','删除失败',5);
		}
	}


/*  +------------------------------------------------------------------------------ */		
}
?>